package mbsl.commons.videos;

import static mbsl.commons.lib.Video.RATING_MAX;
import static mbsl.commons.lib.Video.COLOUR;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import mbsl.commons.Commons;
import mbsl.commons.base.ElementEditDialog;
import mbsl.system.TextProvider;
import mbsl.ui.Combo;
import mbsl.ui.RatingPanel;
import mbsl.ui.Slider;
import mbsl.ui.SpinnerPanel;
import mbsl.ui.UIUtils;

class VideoEditDialog extends ElementEditDialog {
	private static TextProvider tp = Commons.tp.prefix("videos.edit");
	
	public String getName() {
		return getTitleField().getText();
	}
	public void setName(String pName) {
		if (pName == null)
			pName = "";
		
		getTitleField().setText(pName);
	}
	public Text getNameField() {
		return getTitleField();
	}
	/** Returns the rating component */
	Slider getRating() {
		return mRating;
	}
	/** Returns the IMDB Rating component */
	RatingPanel getIMDBRating() {
		return mIMDBRating;
	}
	/** Returns the predecessor combo box */
	Combo getPredecessor() {
		return mPredecessor;
	}
	/** Returns the genre combo box */
	Combo getGenre() {
		return mGenre;
	}
	/** Returns the year spinner */
	SpinnerPanel getYear() {
		return mYear;
	}
	/** Returns the location combo box */
	Combo getLocation() {
		return mLocation;
	}
	/** Add a listener to the IMDB score fetch button */
	void addIMDBFetchListener(SelectionListener pListener) {
		mIMDBFetch.addSelectionListener(pListener);
		mIMDBFetch.setEnabled(true);
	}
	/** Returns the Shell of this dialog */
	public Shell getShell() {
		return mWindow;
	}
	VideoEditDialog(Shell pParentWindow) {
		super(pParentWindow, COLOUR);
		
		getShell().addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent pE) {
				pE.doit = false; // Signals that the system shouldn't continue with the closing operation
				
				close();
			}
		});
	}
	public void close() {
		notifyCloseListeners();
		
		getShell().setVisible(false);
	}
	protected void addFormComponents(Composite pComposite) {
		// Rating
		addLabel(tp.get("rating") + ":");
		mRating = new Slider(mForm, RATING_MAX, SWT.None);
		UIUtils.insertFiller(mForm);
		
		GridData ratingData = getDefaultGridData();
		
		ratingData.heightHint = 22;
		
		mRating.setLayoutData(ratingData);
		// IMDB Rating
		addLabel(tp.get("imdb") + ":");
		mIMDBRating = new RatingPanel(mForm, 0, 10, RatingPanel.DEFAULT, 10);
		mIMDBFetch = new Button(mForm, SWT.PUSH);
		
		mIMDBRating.getWidget().setLayoutData(getDefaultGridData());
		mIMDBFetch.setText(tp.get("imdb.button"));
		mIMDBFetch.setEnabled(false);
		// Predecessor
		addLabel(tp.get("parent") + ":");
		mPredecessor = addCombo(true);
		UIUtils.insertFiller(mForm);
		// Genre
		addLabel(tp.get("genre") + ":");
		mGenre = addCombo(true);
		UIUtils.insertFiller(mForm);
		// Year
		addLabel(tp.get("year") + ":");
		mYear = addYearSpinner(1900);
		UIUtils.insertFiller(mForm);
		// Location
		addLabel(tp.get("location") + ":");
		mLocation = addCombo(true);
		UIUtils.insertFiller(mForm);
	}

	private Slider mRating;
	private RatingPanel mIMDBRating;
	private Button mIMDBFetch;
	private Combo mPredecessor;
	private Combo mGenre;
	private SpinnerPanel mYear;
	private Combo mLocation;
}
